package com.xingshentech.ugv.operate.module.utils;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.xingshentech.ugv.operate.R;
import com.xstech.library.app.BaseApp;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

public class ToastUtil {
    private static Toast toast;
    static TextView tv_toastmsg;
    private static Application getContext() {
        return BaseApp.getApp();
    }
    @SuppressLint("ResourceType")
    public static void showToast(String text, int duration) {
        try {
            if (isActivityRunning(AppUtils.getAppPackageName())) {
                ToastUtils.make()
                        .setBgColor(ColorUtils.getColor(R.color.color_toast))
                        .setTextColor(ColorUtils.getColor(R.color.white))
                        .setGravity(Gravity.BOTTOM, 0, 80)
                        .setDurationIsLong(duration == Toast.LENGTH_LONG)
                        .show(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToast(String text) {
        showToast(text, Toast.LENGTH_SHORT);
    }

    public static void showToast(int resId) {
        showToast(getContext().getString(resId));
    }

    public static boolean isActivityRunning(String packagename) {
        ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = am.getRunningTasks(1);
        String cmpNameTemp = null;
        if (null != runningTaskInfos) {
            cmpNameTemp = runningTaskInfos.get(0).topActivity.toString();
        }
        //判断应用是否在栈顶
        if (null != cmpNameTemp) {
            return cmpNameTemp.contains(packagename);
        }
        return false;
    }
}
